13. Output Devices

Personal work

For this week I worked on the output devices on my Xiao, this time I worked on a code for the srvomotor since in my final project I will mainly use it.

The code that we will use allows us to maintain a position that we indicate, as long as it is within the operating range of the device itself. On the other hand, it allows us to control the rotation speed, we can make it wait a while before moving to the next position.

The first thing, the angle of rotation, in this case allows us to make a sweep between -90º and 90º. Which amounts to a turning angle of 180º.

To begin with, although the servo can move with a resolution of more than 1 degree, this is the maximum resolution that we are going to achieve due to the limitation of the PWM signal that it is capable of generating.

These motors operate with a PWM signal, with a work pulse between 1 ms and 2 ms and a period of 20 ms (50 Hz). What does all this mean? This data tells us the maximum speed at which we can move the servomotor with Arduino. We can only change position every 20 ms. This will depend on the type and brand of our servo.


			#include >Servo.h>
					
				Servo myservo; 
				int pos=0; 
									
			void setup() {
								   
									
				myservo.attach(9);  
				pinMode (2, INPUT):
				}
					
			void loop() {
									
									
				if  (digitalRead(2)==L0W) {
									
				myservo.write (160);  
									
					}
								
				else {
				myservo.write(0); 
					}
									
			}